package com.mall.product.server.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.alibaba.fastjson.JSONArray;
import com.mall.common.core.domain.Result;
import com.mall.product.common.domain.*;
import com.mall.product.server.mapper.*;
import com.mall.product.server.vo.AttrVo;
import com.mall.search.common.domain.Attrs;
import com.mall.common.core.utils.DateUtils;
import com.mall.search.common.domain.SkuEsModel;
import com.mall.search.remote.EsSaveRemoteServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mall.product.server.service.IPmsSpuInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * spu信息Service业务层处理
 *
 * @author mall
 * @date 2024-12-18
 */
@Service
public class PmsSpuInfoServiceImpl implements IPmsSpuInfoService
{
    @Autowired
    private PmsSpuInfoMapper pmsSpuInfoMapper;
    @Autowired
    private PmsSkuInfoMapper pmsSkuInfoMapper;
    @Autowired
    private PmsProductAttrValueMapper pmsProductAttrValueMapper;
    @Autowired
    private PmsBrandMapper pmsBrandMapper;
    @Autowired
    private PmsCategoryMapper pmsCategoryMapper;
    @Autowired
    private EsSaveRemoteServer esSaveRemoteServer;

    /**
     * 查询spu信息
     *
     * @param id spu信息主键
     * @return spu信息
     */
    @Override
    public PmsSpuInfo selectPmsSpuInfoById(Long id)
    {
        return pmsSpuInfoMapper.selectPmsSpuInfoById(id);
    }

    /**
     * 查询spu信息列表
     *
     * @param pmsSpuInfo spu信息
     * @return spu信息
     */
    @Override
    public List<PmsSpuInfo> selectPmsSpuInfoList(PmsSpuInfo pmsSpuInfo)
    {
        return pmsSpuInfoMapper.selectPmsSpuInfoList(pmsSpuInfo);
    }

    /**
     * 新增spu信息
     *
     * @param pmsSpuInfo spu信息
     * @return 结果
     */
    @Override
    public int insertPmsSpuInfo(PmsSpuInfo pmsSpuInfo)
    {
        pmsSpuInfo.setCreateTime(DateUtils.getNowDate());
        return pmsSpuInfoMapper.insertPmsSpuInfo(pmsSpuInfo);
    }

    /**
     * 修改spu信息
     *
     * @param pmsSpuInfo spu信息
     * @return 结果
     */
    @Override
    public int updatePmsSpuInfo(PmsSpuInfo pmsSpuInfo)
    {
        pmsSpuInfo.setUpdateTime(DateUtils.getNowDate());
        return pmsSpuInfoMapper.updatePmsSpuInfo(pmsSpuInfo);
    }

    /**
     * 批量删除spu信息
     *
     * @param ids 需要删除的spu信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuInfoByIds(Long[] ids)
    {
        return pmsSpuInfoMapper.deletePmsSpuInfoByIds(ids);
    }

    /**
     * 删除spu信息信息
     *
     * @param id spu信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuInfoById(Long id)
    {
        return pmsSpuInfoMapper.deletePmsSpuInfoById(id);
    }

    @Transactional
    @Override
    public int updatePublish(Integer id) throws IOException {
        //修改spu上下架状态
        //1.修改数据库spu表上下架状态
        //1.1先在数据库获取spu对象
        PmsSpuInfo pmsSpuInfo = pmsSpuInfoMapper.selectPmsSpuInfoById(Long.valueOf(id));
        Integer publishStatus = pmsSpuInfo.getPublishStatus();
        if(publishStatus==0){
            pmsSpuInfo.setPublishStatus(1);
        }else {
            //todo 下架esSku数据
            pmsSpuInfo.setPublishStatus(0);
        }
        //1.2修改数据库spu表状态
        int i = pmsSpuInfoMapper.updatePmsSpuInfo(pmsSpuInfo);
        //2.根据spuID获取sku列表
        PmsSkuInfo pmsSkuInfo = new PmsSkuInfo();
        pmsSkuInfo.setSpuId(pmsSpuInfo.getId());
        List<PmsSkuInfo> pmsSkuInfos = pmsSkuInfoMapper.selectPmsSkuInfoList(pmsSkuInfo);
        //2.1数据建模 skuInfos->skuEsModels
        List<SkuEsModel>  skuEsModels = transferSkuEsModelList(pmsSkuInfos, Long.valueOf(id));
        //2.2调用esOpenFien 将sku列表对象添加进es
        String jsonString = JSONArray.toJSONString(skuEsModels);
        Result result = esSaveRemoteServer.saveSkuByPublish(jsonString);
        if(result.getCode()!=200){
            throw new RuntimeException("上架失败!");
        }
        return pmsSpuInfo.getPublishStatus();
    }




    ////2.1数据建模 skuInfos->skuEsModels
    private List<SkuEsModel> transferSkuEsModelList(List<PmsSkuInfo> pmsSkuInfos, Long id){
        //1.设置Attrs属性
        //1.1查询spu所有属性列表
        List<PmsProductAttrValue> pmsProductAttrValues = pmsProductAttrValueMapper.selectSearchableBySpuInfoId(id);
        //创建esModel属性列表
        List<Attrs> attrsList = pmsProductAttrValues.stream().map(pmsProductAttrValue -> {
            Attrs attr = new Attrs();
            attr.setAttrId(pmsProductAttrValue.getAttrId());
            attr.setAttrName(pmsProductAttrValue.getAttrName());
            attr.setAttrValue(pmsProductAttrValue.getAttrValue());

            return attr;
        }).collect(Collectors.toList());

        //2设置数据



        List<SkuEsModel> skuEsModels = pmsSkuInfos.stream().map(pmsSkuInfo -> {
            SkuEsModel skuEsModel = new SkuEsModel();
            skuEsModel.setSkuId(pmsSkuInfo.getSkuId());
            skuEsModel.setSkuName(pmsSkuInfo.getSkuName());
            skuEsModel.setSkuImg(pmsSkuInfo.getSkuDefaultImg());
            skuEsModel.setSkuPrice(pmsSkuInfo.getPrice());
            skuEsModel.setBrandId(pmsSkuInfo.getBrandId());
            skuEsModel.setCatalogId(pmsSkuInfo.getCatalogId());
            skuEsModel.setSkuTitle(pmsSkuInfo.getSkuTitle());
            skuEsModel.setSaleCount(pmsSkuInfo.getSaleCount());
            skuEsModel.setHasStock(true);
            skuEsModel.setHotScore(Long.valueOf(100));

            //甚至品牌
            PmsBrand pmsBrand = pmsBrandMapper.selectPmsBrandByBrandId(skuEsModel.getBrandId());
            skuEsModel.setBrandName(pmsBrand.getName());
            skuEsModel.setBrandImg(pmsBrand.getLogo());

            //设置分类
            PmsCategory pmsCategory = pmsCategoryMapper.selectPmsCategoryByCatId(skuEsModel.getCatalogId());
            skuEsModel.setCatalogName(pmsCategory.getName());

            //设置属性
            skuEsModel.setAttrs(attrsList);

            return skuEsModel;
        }).collect(Collectors.toList());


        //3.返回结果集
        return skuEsModels;
    }
}
